<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active{
      color:red;
    }
  </style>
</head>
<body>
  <div id="app">
    <button @click="enterHome">到首页</button>
    <button @click="enterNews">到新闻页</button>
    <button @click="enterAbout">到关于我们页</button>
    <button @click="$router.go(0)">刷新</button>
    <button @click="add_Route">点击添加一个新的路由规则</button>
    <router-link to="/join">到joinus</router-link>
    <hr>
    <!-- 视图上定义 路由组件出口 -->
    <router-view></router-view>
  </div>
  <script src="./vue.js"></script>
  <script src="./vue-router.js"></script>
  <script>
    // 第一步 组件 路由组件的 options 
    const Home = {
      template: `
        <div>
          <h2>首页</h2>  
        </div>
      `,
      mounted(){
        console.log(this.$router, 222);
      }
    }
    const News = {
      template: `
        <div>
          <h2>新闻页</h2>  
        </div>
      `
    }
    const About = {
      template: `
        <div>
          <h2>关于我们页</h2>  
        </div>
      `
    }
    // 定义404组件 路由 options
    const NotFound = {
      template: `
        <div>
          <h2>404啦</h2>  
        </div>
      `
    }
    const JoinUs = {
      template: `
        <div>
          <h2>加入我们</h2>  
        </div>
      `
    }
    // 定义路由 router 实例对象
    const routes = [
      {
        path: '/',
        redirect: '/home'
      },
      {
        path: '/home',
        name: 'home',
        component: Home
      },
      {
        path: '/news',
        name: 'news',
        component: News
      },
      {
        path: '/about',
        name: 'about',
        component: About
      },
      {
        path: '*',
        component: NotFound
      }
    ]
    const router = new VueRouter({
      routes
    })
    console.log(router, 111);
    // 将 路由 挂载实例上
    const vm = new Vue({
      el: '#app',
      methods: {
        enterHome(){
          this.$router.push('/home')
        },
        enterNews(){
          this.$router.push({
            name: 'news'
          })
        },
        enterAbout(){
          this.$router.replace('/about')
        },
        add_Route(){
          
          this.$router.addRoute({
            path: '/join',
            component: JoinUs
          })
        }
      },
      router
    })
  </script>
</body>
</html>